package com.ruoyi.web.controller.laiqianba.merchantdata;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.db.domain.MerchantDataAllocateInfo;
import com.ruoyi.db.domain.MerchantDataPoolInfo;
import com.ruoyi.db.domain.MerchantUserInfo;
import com.ruoyi.merchantdata.service.IMerchantDataAllocateInfoService;
import com.ruoyi.merchantdata.service.IMerchantDataPoolInfoService;
import com.ruoyi.merchantdata.service.IMerchantUserInfoService;
import com.ruoyi.web.wrapper.MemberDataPoolInfoWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 商户客户公海信息Controller
 *
 * @author ruoyi
 * @date 2023-08-24
 */
@RestController
@RequestMapping("/merchantdata/poolInfo")
public class MerchantDataPoolInfoController extends BaseController {
    @Autowired
    private IMerchantDataPoolInfoService merchantDataPoolInfoService;

    @Autowired
    private IMerchantDataAllocateInfoService merchantDataAllocateInfoService;

    @Autowired
    private IMerchantUserInfoService merchantUserInfoService;

    /**
     * 查询商户客户公海信息列表
     */
    @PreAuthorize("@ss.hasPermi('merchantdata:poolInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(MerchantDataPoolInfo merchantDataPoolInfo) {
        startPage();
        List<Map<String, Object>> list = merchantDataPoolInfoService.selectMerchantDataPoolInfo(merchantDataPoolInfo);
        list = new MemberDataPoolInfoWrapper(list).wrap();
        return getDataTable(list);
    }


    /**
     * 获取商户客户公海信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('merchantdata:poolInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(merchantDataPoolInfoService.selectMerchantDataPoolInfoById(id));
    }

    /**
     * 修改商户客户公海信息
     */
    @PreAuthorize("@ss.hasPermi('merchantdata:poolInfo:edit')")
    @Log(title = "商户客户公海信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MerchantDataPoolInfo merchantDataPoolInfo) {
        return toAjax(merchantDataPoolInfoService.updateMerchantDataPoolInfo(merchantDataPoolInfo));
    }

    /**
     * 用户捞取接口
     */
    @PreAuthorize("@ss.hasPermi('merchantdata:poolInfo:laoqu')")
    @Log(title = "用户捞取接口", businessType = BusinessType.UPDATE)
    @PostMapping("/laoqu")
    public AjaxResult laoqu(@RequestBody MerchantDataPoolInfo merchantDataPoolInfo) {
        //判断用户是不是已经被分配 但员工没有刷新页面
        MerchantDataPoolInfo DataPoolInfo = merchantDataPoolInfoService.selectMerchantDataPoolInfoById(merchantDataPoolInfo.getId());
        Long Uid = DataPoolInfo.getUid();
        MerchantDataAllocateInfo info = new MerchantDataAllocateInfo();
        info.setUserId(SecurityUtils.getUserId());
        info.setUid(Uid);
        MerchantDataAllocateInfo memeber = merchantDataAllocateInfoService.selectMerchantDataAllocateInfoByUidAndUserId(info);
        if (memeber != null && memeber.getUserId().equals(SecurityUtils.getUserId())) {
            MerchantUserInfo userInfo = merchantUserInfoService.selectMerchantUserInfoById(SecurityUtils.getUserId());
            String username = "";
            if (userInfo != null) {
                username =userInfo.getNickName();
            }
            return AjaxResult.error("您选取的用户已经被分配到" +username+"名下，请勿重新捞取 刷新页面以获取最新数据");
        }
        return toAjax(merchantDataPoolInfoService.laoquMerchantDataPoolInfo(merchantDataPoolInfo));
    }

    /**
     * 分配用户接口
     */
    @PreAuthorize("@ss.hasPermi('merchantdata:poolInfo:fenpei')")
    @Log(title = "用户分配接口", businessType = BusinessType.UPDATE)
    @PostMapping("/fenpei")
    public AjaxResult fenpei(@RequestBody MerchantDataPoolInfo merchantDataPoolInfo) {
        //判断用户是不是已经被捞取 但管理员刷新页面
        MerchantDataPoolInfo DataPoolInfo = merchantDataPoolInfoService.selectMerchantDataPoolInfoById(merchantDataPoolInfo.getId());
        Long Uid = DataPoolInfo.getUid();
        MerchantDataAllocateInfo info = new MerchantDataAllocateInfo();
        info.setUserId(merchantDataPoolInfo.getUserId());
        info.setUid(Uid);
        MerchantDataAllocateInfo memeber = merchantDataAllocateInfoService.selectMerchantDataAllocateInfoByUidAndUserId(info);
        if (memeber != null && memeber.getUserId().equals(merchantDataPoolInfo.getUserId())) {
            MerchantUserInfo userInfo = merchantUserInfoService.selectMerchantUserInfoById(merchantDataPoolInfo.getUserId());
            String username = "";
            if (userInfo != null) {
                username =userInfo.getNickName();
            }
            return AjaxResult.error("您选取的用户已经被分配到" +username+"名下，请勿重新分配 刷新页面以获取最新数据");
        }
        return toAjax(merchantDataPoolInfoService.fenpeiMerchantDataPoolInfo(merchantDataPoolInfo));
    }

    @PreAuthorize("@ss.hasPermi('merchantdata:poolInfo:batchFenpei')")
    @Log(title = "批量用户分配接口", businessType = BusinessType.UPDATE)
    @PostMapping("/batchFenpei")
    public AjaxResult batchFenpei(@RequestBody List<MerchantDataPoolInfo> merchantDataPoolInfoList) {
        int successCount = 0;

        for (MerchantDataPoolInfo dataPoolInfo : merchantDataPoolInfoList) {
            int result = merchantDataPoolInfoService.fenpeiMerchantDataPoolInfo(dataPoolInfo);
            if (result > 0) {
                successCount++;
            }
        }

        return AjaxResult.success("成功分配 " + successCount + " 条数据");
    }

}
